﻿
//Author: 张亚涛
//createtime:2011-10-13 10:35:35
//Description:

using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Model;
using NS.DAL.Systems;


namespace DAL
{
    /// <summary>
    /// 该类提供了一系列操作 "CarType" 表的方法;
    /// </summary>
    public class CarTypeBase
    {
        const string SQL_Select = "SELECT [TypeID],[TypeName],[Letter],[Logo],[ParentID],[ParentPath], [Depth],[OrderID] FROM [CarType] ";
        #region .对本表的维护.
        #region select data
        /// <summary>
        /// 返回CarType表的所有数据
        /// <param name="ParentID">父ID</param>
        /// </summary>
        /// <returns></returns>        
        public static List<CarType> GetAllCarTypeByParentID(long ParentID)
        {
            List<CarType> carTypeList = new List<CarType>();
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [ParentID] = @ParentID ORDER BY OrderID";
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@ParentID",ParentID)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            while (reader.Read())
            {
                CarType carType = new CarType(reader);
                carTypeList.Add(carType);
            }
            reader.Close();
            return carTypeList;
        }
        /// <summary>
        /// 返回车型首字母
        /// </summary>
        /// <returns></returns>        
        public static List<string> GetCarTypeLetter()
        {
            List<string> carTypeList = new List<string>();
            SqlDataReader reader = null;

            string sql = " select DISTINCT(Letter)  from CarType where [ParentID] = 0 ORDER BY Letter";
            reader = SQLHelper.ExecuteDataReader(sql, null);
            while (reader.Read())
            {
                carTypeList.Add(reader[0].ToString());
            }
            reader.Close();
            return carTypeList;
        }
        /// <summary>
        /// 根据首字母返回品牌列表
        /// <param name="Letter">首字母</param>
        /// </summary>
        /// <returns></returns>        
        public static List<CarType> GetCarTypeByLetter(string Letter)
        {
            List<CarType> carTypeList = new List<CarType>();
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [ParentID] = 0 and Letter='" + Letter + "'";
            reader = SQLHelper.ExecuteDataReader(sql, null);
            while (reader.Read())
            {
                CarType carType = new CarType(reader);
                carTypeList.Add(carType);
            }
            reader.Close();
            return carTypeList;
        }
        /// <summary>
        /// 根据CarType表的TypeID字段返回数据 
        /// </summary>
        /// <param name="typeID">车型ID</param>
        /// <returns></returns>       
        public static CarType GetCarTypeByTypeID(long typeID)
        {
            CarType carType = null;
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [TypeID] = @TypeID";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@TypeID",typeID)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            if (reader.Read())
            {
                carType = new CarType(reader);
            }
            reader.Close();
            return carType;
        }

        /// <summary>
        /// 根据车型ID返回车型名称 
        /// </summary>
        /// <param name="typeID">车型ID</param>
        /// <returns></returns>       
        public static string GetTypeNameByTypeID(int typeID)
        {
            string sql = "SELECT [TypeName] FROM [CarType] WHERE [TypeID] = @TypeID";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@TypeID",typeID)
            };
            return SQLHelper.ExecuteScalar(sql, paras);
        }

        /// <summary>
        /// 根据车型ID返回车型LOGO 
        /// </summary>
        /// <param name="typeID">车型ID</param>
        /// <returns></returns>       
        public static string GetLogoByTypeID(int typeID)
        {
            string sql = "SELECT [Logo] FROM [CarType] WHERE [TypeID] = @TypeID";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@TypeID",typeID)
            };
            return SQLHelper.ExecuteScalar(sql, paras);
        }

        #endregion
        #region delete data
        /// <summary>
        /// 根据CarType表的TypeID字段删除数据 
        /// </summary>
        /// <param name="typeID">typeID</param>
        /// <returns></returns>        
        public static int DeleteCarTypeByTypeID(long typeID)
        {
            string sql = "delete from [CarType] where [TypeID] = @TypeID";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@TypeID",typeID)
            };
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        /// <summary>
        /// 根据CarType表的ParentID字段删除数据 
        /// </summary>
        /// <param name="ParentID">ParentID</param>
        /// <returns>返回受影响的行数</returns>
        public static int DeleteCarTypeByParentID(long ParentID)
        {
            string sql = "delete from [CarType]";
            SqlParameter[] paras ={
                                         new SqlParameter("@ParentID","%," + ParentID + ",%") 
                                       };
            return SQLHelper.ExecuteNonQuery(sql + " Where (',' + ParentPath + ',' LIKE @ParentID)", paras);
        }
        #endregion
        #region update data
        /// <summary>
        /// prepare parameters 
        /// </summary>
        public static SqlParameter[] PrepareCommandParameters(CarType carType)
        {
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@TypeID",carType.TypeID),
            new SqlParameter("@TypeName",carType.TypeName),
            new SqlParameter("@Letter",carType.Letter),
            new SqlParameter("@Logo",carType.Logo),
            new SqlParameter("@ParentID",carType.ParentID),
            new SqlParameter("@ParentPath",carType.ParentPath),
            new SqlParameter("@Depth",carType.Depth),
            new SqlParameter("@OrderID",carType.OrderID)
            };
            return paras;
        }
        /// <summary>
        /// 根据CarType表的TypeID字段更新数据 
        /// </summary> 
        /// <param name="CarType">carType</param>
        /// <returns></returns>       
        public static int UpdateCarTypeByTypeID(CarType carType)
        {
            string sql = "update [CarType] set [TypeName] = @TypeName,[Letter] = @Letter,[Logo] = @Logo,[OrderID] = @OrderID where [TypeID] = @TypeID";
            SqlParameter[] paras = PrepareCommandParameters(carType);
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region insert data
        /// <summary>
        /// 向CarType表插入一条数据，返回自增列数值，插入不成功则返回-1
        /// </summary>
        /// <param name="carType">CarType</param>       
        /// <returns></returns>        
        public static int AddCarType(CarType carType)
        {
            string sql = "insert into [CarType]([TypeName],[Letter],[Logo],[ParentID],[ParentPath],[Depth],[OrderID])values(@TypeName,@Letter,@Logo,@ParentID,@ParentPath,@Depth,@OrderID);select @@identity";
            SqlParameter[] paras = PrepareCommandParameters(carType);
            int id = -1;

            string _ID = SQLHelper.ExecuteScalar(sql, paras).ToString();
            int.TryParse(_ID, out id);

            return id;
        }
        #endregion
        #endregion

        #region .SQL语句汇总.
        /// <summary>
        /// 返回CarType表的所有数据 （SQL语句）
        /// <param name="ParentID">父ID</param>
        /// </summary>
        /// <returns></returns>        
        public static string GetAllCarType_SQL(string ParentID)
        {
            string strSQL = SQL_Select + " WHERE ParentID='" + ParentID + "'";
            return strSQL;
        }
        #endregion
    }
}
